{
<<<<<<< HEAD
 "cells": [],
 "metadata": {},
=======
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import glovedata\n",
    "import tensorglove_osc_client\n",
    "import tensorglove_osc_server\n",
    "import tensorglove\n",
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "Run the training and serving.\n",
    ":param argv: Command line arguments.\n",
    ":return:\n",
    "\"\"\"\n",
    "args = parser.parse_args(argv[1:])\n",
    "\n",
    "# Fetch the data\n",
    "(train_x, train_y), (test_x, test_y) = glovedata.load_data()\n",
    "\n",
    "# Feature columns describe how to use the input.\n",
    "my_feature_columns = []\n",
    "for key in train_x.keys():\n",
    "    my_feature_columns.append(tf.feature_column.numeric_column(key=key))\n",
    "\n",
    "hidden_units = [12, 10]\n",
    "\n",
    "# Build 2 hidden layer DNN with 10, 10 units respectively.\n",
    "classifier = tf.estimator.DNNClassifier(\n",
    "    feature_columns=my_feature_columns,\n",
    "    # Two hidden layers of 10 nodes each.\n",
    "    hidden_units=hidden_units,\n",
    "    # The model must choose between 4 classes.\n",
    "    n_classes=4,\n",
    "    model_dir=\"model_{0}_{1}\".format(hidden_units[0], hidden_units[1]))\n",
    "\n",
    "# Train the Model.\n",
    "classifier.train(\n",
    "    input_fn=lambda: glovedata.train_input_fn(train_x, train_y,\n",
    "                                              args.batch_size),\n",
    "    steps=args.train_steps)\n",
    "\n",
    "# Evaluate the model.\n",
    "eval_result = classifier.evaluate(\n",
    "    input_fn=lambda: glovedata.eval_input_fn(test_x, test_y,\n",
    "                                             args.batch_size))\n",
    "\n",
    "print('\\nTest set accuracy: {accuracy:0.3f}\\n'.format(**eval_result))\n",
    "\n",
    "# perform a sample prediction\n",
    "\n",
    "# Generate predictions from the model\n",
    "expected = ['Fist']\n",
    "feature_values = [0, 0, 0, 1, 0.2298394, 0.9228836, -0.1887825, -0.2445833, 0.03349165, -0.2397906, -0.1800468,\n",
    "                  0.9533949, -0.07355202, 0.2745002, -0.2481479, 0.9261007, -4.470348E-08, 0.2213377, 1.490116E-08,\n",
    "                  0.9751973, -2.235174E-08, -5.960464E-08, 4.470348E-08, 1, 0.09148686, -0.09309785, -0.6949129,\n",
    "                  0.7071485, 7.105427E-15, 7.45058E-08, -0.7191889, 0.6948146, 1.490116E-08, -1.490116E-08,\n",
    "                  -0.566272, 0.8242185, -2.235174E-08, -5.960464E-08, 4.470348E-08, 1, -3.725291E-08, 5.960464E-08,\n",
    "                  -0.7009093, 0.7132504, 1.490116E-08, 3.352762E-08, -0.772563, 0.6349381, -7.450572E-09,\n",
    "                  2.980232E-08, -0.6148145, 0.7886717, -2.235174E-08, -5.960464E-08, 4.470348E-08, 1, -0.06108833,\n",
    "                  0.06216392, -0.6982422, 0.7105362, -3.352762E-08, 1.490116E-08, -0.7539417, 0.6569413,\n",
    "                  5.960465E-08, 5.960463E-08, -0.5976215, 0.8017784, -2.235174E-08, -5.960464E-08, 4.470348E-08, 1,\n",
    "                  -0.1217117, 0.1238547, -0.6902609, 0.7024146, -5.029142E-08, 3.725291E-08, -0.819152, 0.5735765,\n",
    "                  5.215406E-08, 5.960463E-08, -0.6427875, 0.7660446]\n",
    "# the data is expected to be a list of feature values (as it is configured for batching)\n",
    "predict_x = dict(zip(FEATURES, [[x] for x in feature_values]))\n",
    "\n",
    "predictions = classifier.predict(\n",
    "    input_fn=lambda: glovedata.eval_input_fn(predict_x,\n",
    "                                             labels=None,\n",
    "                                             batch_size=args.batch_size))\n",
    "\n",
    "template = '\\nPrediction is \"{}\" ({:.1f}%), expected \"{}\"'\n",
    "\n",
    "for pred_dict, expec in zip(predictions, expected):\n",
    "    class_id = pred_dict['class_ids'][0]\n",
    "    probability = pred_dict['probabilities'][class_id]\n",
    "\n",
    "    print(template.format(glovedata.GESTURES[class_id],\n",
    "                          100 * probability, expec))\n",
    "\n",
    "export_dir = classifier.export_savedmodel(\n",
    "    export_dir_base=\"model_export\",\n",
    "    serving_input_receiver_fn=serving_input_receiver_fn)\n",
    "print('Exported to:', export_dir)\n",
    "\n",
    "if args.run_server:\n",
    "    run_server(classifier)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
>>>>>>> 17c7d269e790d99e1c8583e3576115b5cc12aecf
 "nbformat": 4,
 "nbformat_minor": 2
}
